//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.TridentUtilities
{
    #region Using Directives.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Security;

    #endregion Using Directives.

    /// <summary>
    /// Holds the basic utility helpers for the workflows.
    /// </summary>
    public static class WorkflowUtilities
    {
        /// <summary>
        /// The file path for the temp xoml file.
        /// </summary>
        private static string tempXomlFilePath = string.Empty;

        /// <summary>
        /// The name of the temporary directory.
        /// </summary>
        private static string tempDirName = "TridentTemp";

        /// <summary>
        /// Holds the path of the tempDir in the app folder.
        /// </summary>
        private static string tempDirPath = string.Empty;

        /// <summary>
        /// The name of the temp file.
        /// </summary>
        private static string tempFileName = "temp";

        /// <summary>
        /// The extension of the temp file.
        /// </summary>
        private static string tempFileExtension = "xoml";

        /// <summary>
        /// Static Constructor.
        /// </summary>
        static WorkflowUtilities()
        {
            string tempFileNameWithExtn = WorkflowUtilities.tempFileName + "." + WorkflowUtilities.tempFileExtension;
            WorkflowUtilities.tempXomlFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, tempFileNameWithExtn);
            string tempPath = Path.GetTempPath();
            WorkflowUtilities.TempDirPath = System.IO.Path.Combine(tempPath, WorkflowUtilities.tempDirName);
        }

        /// <summary>
        /// Gets or sets the file path for the temp xoml file. In case of the file not being found at the specified path,
        /// will try to create the file. IOException will be thrown in case of failure to create.
        /// </summary>
        public static string TempXomlFilePath
        {
            get
            {
                return WorkflowUtilities.tempXomlFilePath;
            }

            set
            {
                string path = value;
                WorkflowUtilities.tempXomlFilePath = System.IO.Path.GetFullPath(path);
                if (!System.IO.Path.IsPathRooted(WorkflowUtilities.tempXomlFilePath))
                {
                    throw new IOException();
                }

                if (!File.Exists(WorkflowUtilities.tempXomlFilePath))
                {
                    File.Create(WorkflowUtilities.tempXomlFilePath);
                }
            }
        }

        /// <summary>
        /// Gets the path of the temporary directory in the Trident application folder.
        /// </summary>
        public static string TempDirPath
        {
            get
            {
                return WorkflowUtilities.tempDirPath;
            }

            private set
            {
                string path = value;
                DirectoryInfo dInfo = new DirectoryInfo(path);
                if (!dInfo.Exists)
                {
                    try
                    {
                        dInfo.Create();
                        dInfo.Attributes = FileAttributes.Hidden;
                    }
                    catch (IOException)
                    {
                        dInfo = null;
                    }
                }

                if (dInfo != null)
                {
                    WorkflowUtilities.tempDirPath = dInfo.FullName;
                }
                else
                {
                    WorkflowUtilities.tempDirPath = string.Empty;
                }
            }
        }
    }
}
